package com.lzy.cloud.common.controller;

import com.lzy.cloud.common.dtos.ParameterDto;
import com.lzy.cloud.common.request.ParameterRequest;
import com.lzy.cloud.common.response.ParameterResponse;
import com.lzy.cloud.common.service.ParameterService;
import com.lzy.framework.core.ensure.Ensure;
import com.lzy.framework.core.output.Result;
import com.lzy.framework.core.utils.DozerUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/common")
public class ParameterController {

    @Autowired
    private ParameterService parameterService;

    /**
     * 查询
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "/select/id", method = RequestMethod.GET)
    public Result selectById(@RequestParam Long id) {
        return new Result(DozerUtils.map(parameterService.selectById(id), ParameterResponse.class));
    }

    /**
     * 添加
     *
     * @param parameterRequest
     * @return
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public Result insert(@RequestBody ParameterRequest parameterRequest) {
        Boolean flag = parameterService.insert(DozerUtils.map(parameterRequest, ParameterDto.class));
        Ensure.that(flag).isTrue("00000003");
        return new Result(true);
    }

    /**
     * 修改
     *
     * @param parameterRequest
     * @return
     */
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public Result update(@RequestBody ParameterRequest parameterRequest) {
        Boolean flag = parameterService.update(DozerUtils.map(parameterRequest, ParameterDto.class));
        Ensure.that(flag).isTrue("00000004");
        return new Result(true);
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "/delete", method = RequestMethod.GET)
    public Result delete(@RequestParam Long id) {
        Boolean flag = parameterService.delete(id);
        Ensure.that(flag).isTrue("00000005");
        return new Result(true);
    }

    /**
     * 根据系统编码查询
     *
     * @param parameterRequest
     * @return
     */
    @RequestMapping(value = "/query/code", method = RequestMethod.POST)
    public Result queryByCode(@RequestBody ParameterRequest parameterRequest) {
        List<ParameterDto> list = parameterService.queryByCode(DozerUtils.map(parameterRequest, ParameterDto.class));
        return new Result(DozerUtils.maps(list, ParameterResponse.class));
    }

}
